using System;
using System.Collections;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Pattern;

public class DuelCompleteUI : MonoBehaviour
{
	public Image PlayerBar;

	public Image OpponentBar;

	public Image OpponentFlag;

	public RectTransform BarParent;

	public RectTransform BarSeparator;

	public Text TitleText;

	public TextMeshProUGUI PlayerName;

	public TextMeshProUGUI PlayerCash;

	public TextMeshProUGUI OpponentName;

	public TextMeshProUGUI OpponentCash;

	public TextMeshProUGUI MainCash;

	private float _playerMoney;

	private float _opponentMoney;

	private float _totalMoney;

	public GameObject WinningObject;

	public Button NextFightButton;

	public Button ReturnToMain;

	public GamifiedRVView RVView;

	public TextMeshProUGUI MultipliedCash;

	public GameObject RewardFX;

	private CanvasGroup _canvasGroup;

	private void DisplayFX()
	{
		RewardFX.SetActive(value: false);
		RewardFX.SetActive(value: true);
	}

	private void Start()
	{
		_canvasGroup = GetComponent<CanvasGroup>();
		NextFightButton.onClick.AddListener(delegate
		{
			SingletonMB<GameManager>.Instance.LoadIntoDuel();
			NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) + _playerMoney);
		});
		ReturnToMain.onClick.AddListener(delegate
		{
			SingletonMB<GameManager>.Instance.LoadIntoMain();
			NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) + _playerMoney);
		});
		Hide();
		GamifiedRVView rVView = RVView;
		rVView.OnClicked = (Action<int>)Delegate.Combine(rVView.OnClicked, (Action<int>)delegate(int multiplier)
		{
			_playerMoney *= multiplier;
			SingletonMB<GameManager>.Instance.LoadIntoDuel();
			RVView.gameObject.SetActive(value: false);
			StopCoroutine(WaitForGamified());
			SetupBar();
			if (multiplier > 1)
			{
				DisplayFX();
			}
		});
	}

	private void RestartDuel()
	{
		SingletonMB<GameManager>.Instance.LoadIntoDuel();
		NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) + _playerMoney);
	}

	public void Show()
	{
		_canvasGroup.blocksRaycasts = true;
		_canvasGroup.interactable = true;
		_canvasGroup.DOFade(1f, 0.25f);
	}

	public void Hide()
	{
		_canvasGroup.blocksRaycasts = false;
		_canvasGroup.interactable = false;
		_canvasGroup.alpha = 0f;
	}

	public void Fill(float playerMoney, float opponentMoney)
	{
		TitleText.text = ((playerMoney > opponentMoney) ? "You Won!" : "So Close!");
		OpponentFlag.sprite = SingletonMB<DuelModeManager>.Instance._opponentSprite;
		OpponentName.text = SingletonMB<DuelModeManager>.Instance.OpponentName;
		if (playerMoney < opponentMoney && playerMoney / opponentMoney < 0.3f)
		{
			playerMoney = (int)(opponentMoney / 2f);
		}
		else if (opponentMoney < playerMoney && opponentMoney / playerMoney < 0.3f)
		{
			opponentMoney = (int)(playerMoney / 2f);
		}
		if (playerMoney > opponentMoney)
		{
			WinningObject.SetActive(value: true);
		}
		else
		{
			WinningObject.SetActive(value: false);
		}
		_playerMoney = 0f;
		_opponentMoney = 0f;
		_totalMoney = 1f;
		SetupBar();
		_playerMoney = playerMoney;
		_opponentMoney = opponentMoney;
		_totalMoney = playerMoney + opponentMoney;
		BarSeparator.gameObject.SetActive(value: false);
		StartCoroutine(RewardWinner());
	}

	private void SetupBar()
	{
		PlayerCash.text = ((int)_playerMoney).ToString() ?? "";
		OpponentCash.text = ((int)_opponentMoney).ToString() ?? "";
		PlayerBar.fillAmount = _playerMoney / _totalMoney;
		OpponentBar.fillAmount = _opponentMoney / _totalMoney;
		BarSeparator.DOAnchorPosX(BarParent.rect.size.x * (_playerMoney / _totalMoney) - BarParent.rect.size.x / 2f, 0f, snapping: true);
		MainCash.text = (int)_playerMoney + "$";
	}

	private IEnumerator RewardWinner()
	{
		OpponentBar.fillAmount = 0f;
		PlayerBar.fillAmount = 0f;
		RewardFX.SetActive(value: false);
		NextFightButton.gameObject.SetActive(value: false);
		ReturnToMain.gameObject.SetActive(value: false);
		RVView.gameObject.SetActive(value: false);
		yield return new WaitForSeconds(0.5f);
		float timer = 0.5f;
		float num = _playerMoney + _opponentMoney;
		float playerTargetFill = _playerMoney / num;
		float opponentTargetFill = _opponentMoney / num;
		PlayerBar.DOFillAmount(playerTargetFill, 1f).OnUpdate(delegate
		{
			PlayerCash.text = (int)Mathf.Lerp(0f, _playerMoney, PlayerBar.fillAmount / playerTargetFill) + "$";
		});
		OpponentBar.DOFillAmount(opponentTargetFill, 1f).OnUpdate(delegate
		{
			OpponentCash.text = (int)Mathf.Lerp(0f, _opponentMoney, OpponentBar.fillAmount / opponentTargetFill) + "$";
		});
		yield return new WaitForSeconds(1.25f);
		BarSeparator.gameObject.SetActive(value: true);
		SetupBar();
		if (_playerMoney > _opponentMoney)
		{
			float num2 = ((_opponentMoney / _playerMoney > 0.5f) ? 100f : 50f);
			float delta = _totalMoney / num2;
			while (_opponentMoney > 0f)
			{
				if (_opponentMoney < delta)
				{
					_playerMoney += _opponentMoney;
					_opponentMoney = 0f;
				}
				else
				{
					_playerMoney += delta;
					_opponentMoney -= delta;
				}
				if (timer <= 0f)
				{
					SingletonMB<GameManager>.Instance.GameplayHaptic.PlayHaptic(HapticType.Small);
					timer = 0.5f;
				}
				yield return null;
				if (_opponentMoney / _playerMoney < 0.1f)
				{
					BarSeparator.gameObject.SetActive(value: false);
				}
				timer -= Time.deltaTime;
				SetupBar();
			}
		}
		else
		{
			yield return new WaitForSeconds(2f);
		}
		SingletonMB<DuelModeManager>.Instance.ShowLeaderboard(delegate
		{
			SingletonMB<DuelModeManager>.Instance.HideLeaderboard();
			RVView.gameObject.SetActive(value: true);
			StartCoroutine(WaitForGamified());
		});
		if (_playerMoney > _opponentMoney)
		{
			DisplayFX();
		}
	}

	private IEnumerator WaitForGamified()
	{
		while (true)
		{
			MultipliedCash.text = "+" + (int)_playerMoney * RVView.GetValue() + "$";
			yield return null;
		}
	}

	private void Update()
	{
	}
}
